package com.atguigu.yygh.hosp.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.DepartmentVo;

@Service
public class DepartmentServiceImpl implements DepartmentService{

	@Autowired
	private DepartmentRepository departmentRepository;

	@Override
	public void save(Map<String, Object> paramMap) {
		// map 集合转换成对象    先转换成字符串
		String json = JSONObject.toJSONString(paramMap);
		//转换成 Hospital 对象
		Department department = JSONObject.parseObject(json,Department.class);
		//1.判断是否存在数据
		String hostcode = department.getHostcode();
		String depcode = department.getDepcode();
		Department departmentExit = departmentRepository.getDepartmentByHostcodeAndDepcode(hostcode,depcode);
		//存在   修改操作
		if(departmentExit != null){
			department.setUpdateTime(new Date());
			department.setIsDeleted(0);
			departmentRepository.save(department);
		}else{
		//不存在  添加操作
			department.setUpdateTime(new Date());
			department.setCreateTime(new Date());
			department.setIsDeleted(0);
			departmentRepository.save(department);
		}
		
	}

	@Override
	public Page<Department> findDepartmentPage(int page, int limit, DepartmentQueryVo departmentQueryVo) {
		//创建 pageable 对象  设置当前页  每页记录数
		Pageable pageable = PageRequest.of(page - 1, limit);
		//创建 Example<T> 对象 
		ExampleMatcher marExampleMatcher = ExampleMatcher.matching()
				.withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING).withIgnoreCase(true);
		Department department = new Department();
		BeanUtils.copyProperties(departmentQueryVo, department);
		department.setIsDeleted(0);
		department.setHostcode(departmentQueryVo.getHostcode());
		Example<Department> example = Example.of(department,marExampleMatcher);
		//分页 构建部分   
		//匹配规则后    查询记录数  多少条记录
		long count = departmentRepository.count();
		System.out.println(count);
		//分页
		Page<Department> deList = departmentRepository.findAll(example, pageable);
		return deList;
	}

	@Override
	public void remove(String hostcode, String depcode) {
		// TODO Auto-generated method stub
		//根据 医院编号 科室编号查询
		Department departmentExit = departmentRepository.getDepartmentByHostcodeAndDepcode(hostcode,depcode);
		//存在  获取id值 进行删除
		if(departmentExit != null){
			departmentRepository.deleteById(departmentExit.getId());
		}
		//不存在   不需要删除
	}

	@Override
	public List<DepartmentVo> findDeptTree(String hostcode) {
		// TODO Auto-generated method stub
		//创建 List 集合 用于数据的返回 封装
		List<DepartmentVo> deList = new ArrayList<DepartmentVo>();
		//根据医院编号  查询全部科室信息
		Department department = new Department();
		department.setHostcode(hostcode);
		Example<Department> example = Example.of(department);
		//获取 所有科室列表  departmentList
		List<Department> departmentList = departmentRepository.findAll(example);
		
		//根据大类别科室 做分组    内科/外科 .....   大科室编号   bigcode分组  
		//利用  java8 stream流的方式（针对集合或者数组进行操作  计算、筛选，结果的输出）  获取大科室下   下级子科室
		Map<String,List<Department>> collect =
				departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
		//map 集合遍历
		for(Map.Entry<String, List<Department>> entry : collect.entrySet()){
			//大科室编号
			String bigCode = entry.getKey();
			//大科室编号   对应的全部科室数据
			List<Department> departList = entry.getValue();
			
			//封装大科室
			DepartmentVo departmentVo = new DepartmentVo();
			departmentVo.setDepcode(bigCode);
			departmentVo.setDepname(departList.get(0).getBigname());
			//再 封装小科室
			List<DepartmentVo> children =new ArrayList<>();
			for(Department depart : departList){
				DepartmentVo departmentVo2 = new DepartmentVo();
				departmentVo2.setDepcode(depart.getDepcode());
				departmentVo2.setDepname(depart.getDepname());
				//封装到list集合中
				children.add(departmentVo2);
			}
			//将 小科室 信息  封装到 大科室
			departmentVo.setChildren(children);
			//放到最终返回的list中
			deList.add(departmentVo);
		}
		return deList;
	}

	//获取科室名称
	@Override
	public String getDepName(String hostcode, String depcode) {
		// TODO Auto-generated method stub
		Department department = departmentRepository.getDepartmentByHostcodeAndDepcode(hostcode, depcode);
		if(department.getDepname() != null){
			return department.getDepname();
		}
		return null;
	}

}
